package com.un.ebs.customer.event;

import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.customer.dto.CustomerTraceAddDto;
import com.un.ebs.customer.service.CustomerTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Async
public class CustomerTraceListener {

    @Resource
    private CustomerTraceService customerTraceService;

    @EventListener
    public void onApplicationEvent(CustomerTraceEvent event) {
        CustomerTraceAddDto req = new CustomerTraceAddDto();
        req.setCustomerId(event.getCustId());
        req.setTraceDict(event.getTypeDict());
        req.setPlanDate(event.getPlanDate());
        req.setRemark(event.getRemark());
        req.setRemindRemark(event.getRemindRemark());
        if(StringUtils.isNullOrEmpty(event.getPersonnelId())){
            if(event.getPlanDate()==null){
                req.setPersonnelId(SecurityUtils.getUserContent().getPersonnelId());
            }
        }else{
            req.setPersonnelId(event.getPersonnelId());
        }
        customerTraceService.add(req);
    }
}
